package com.houmeiyuan.huiguniang.service;

import com.houmeiyuan.huiguniang.modles.dao.OrderFormDao;
import com.houmeiyuan.huiguniang.modles.dao.OrderItemDao;
import com.houmeiyuan.huiguniang.modles.domain.OrderDetail;
import com.houmeiyuan.huiguniang.modles.domain.OrderForm;
import com.houmeiyuan.huiguniang.modles.domain.ShopCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderFormService {
    @Autowired
    public OrderFormDao orderformdao;
    @Autowired
    private OrderItemDao orderItemdao;

    /**
     * 通过id查询状态信息
     *
     * @param id
     * @return
     */
    public OrderForm selectById(Integer id){
        return orderformdao.selectById(id);
    }
    /**
     * 根据orderFormId删除对应订单
     *
     * @param orderFormId
     * @param openId
     * @return
     */
    @Transactional
    public int DeleteByOrderFormId(String openId,int orderFormId) {
        return orderformdao.DeleteByOrderFormId(openId,orderFormId);
    }

    @Transactional
    public List<OrderDetail> selectAll(Map<String, Object> params) {
        List<OrderDetail> orderDetails = new ArrayList<>();
        // 根据 openid查出所有的oderform
        List<OrderForm> orderFormList = orderformdao.selectAll(params);
        System.out.println("-------------------------------------------------------------------------");
        System.out.println(orderFormList.size());
        //    for循环通过订单id查出所有订单项的内容
        for (OrderForm orderform : orderFormList) {
            List<ShopCar> list = orderItemdao.searchByOrderNumber(orderform.getId());
            OrderDetail orderDetail = new OrderDetail(orderform, list);
            orderDetails.add(orderDetail);
        }
        System.out.println("-------------------------------------------------------------------------");
        System.out.println(orderDetails.toString());
        return orderDetails;
    }

    @Transactional
    public Integer updateOrder(OrderForm orderform) {
        return orderformdao.updateOrder(orderform);
    }
}
